package movie.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import movie.pojo.Cart;
import movie.pojo.Movie;
import movie.pojo.Result;
import movie.pojo.User;
import movie.service.CartService;
import movie.service.MovieService;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
@RequestMapping("/cart")
public class CartController {
    //controller层调用service层
    @Autowired
    private MovieService movieService;

    @Autowired
    private CartService cartService;

    @GetMapping("/add")
    @ResponseBody
    public Result addCart(Integer id, HttpServletRequest req){
        //设置购物的拥有者user_id
        //因为登录之后,user存储在session作用域中,从作用域中得到这个登录的User对象的
        HttpSession session = req.getSession();
        User user = (User) session.getAttribute("user");

        //1. 根据id查询单个product
        Movie movie = movieService.getById(id);

        //校验user_id和product_id在购物车表中是否同时具备唯一性
        //如果某个人已经添加了某个产品的话,后续的添加的动作,更新count+1
        //涉及到条件查询
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("movie_id",id).eq("user_id",user.getId());

        Cart c = cartService.getOne(queryWrapper);

        if(c!=null){ // 曾经已经添加过...
            UpdateWrapper<Cart> updateWrapper = new UpdateWrapper<>();

            updateWrapper.eq("movie_id",id).eq("user_id",user.getId());

            c.setCounts(c.getCounts()+1);

            c.setPrice(movie.getPrice()*c.getCounts());

            //进行更新操作
            boolean b = cartService.update(c,updateWrapper);

            if(b){
                return new Result("200", "添加购物车成功", null);
            }

            return new Result("500", "添加购物车失败", null);
        }

        //头一次添加..
        //2. 创建Cart对象
        Cart cart = new Cart();
        cart.setMovieId(id);
        cart.setName(movie.getName());
        cart.setImg(movie.getImg());
        cart.setMoviePrice(movie.getPrice());//产品的单价
        cart.setCounts(1);//加入购车的数量...
        cart.setPrice(movie.getPrice()*cart.getCounts());//购物车的总价


        cart.setUserId(user.getId());

        //end - 调用保存方法

        boolean flag = cartService.save(cart);

        if(flag) {
            return new Result("200", "添加购物车成功", null);
        }
        return new Result("500", "添加购物车失败", null);
    }


    @GetMapping("/index")
    public String index(HttpServletRequest req){
        //构建一个session对象
        HttpSession session = req.getSession();

        //从session作用域中获取user对象
        User user = (User) session.getAttribute("user");

        //根据user_id来查询购物车列表
        QueryWrapper<Cart> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",user.getId());

        //查询购物车表中的所有数据并且加载出来.
        List<Cart> cartList = cartService.list(queryWrapper);

        //将这个集合对象放入到request作用域中
        session.setAttribute("cartList",cartList);

        //转发到/cart/index.jsp
        return  "cart/index";

    }

    @GetMapping("/del")
    public String del(Integer id){
        //根据id进行删除操作
        cartService.removeById(id);
        //思考跳转
        return "redirect:/cart/index";
    }

    @GetMapping("/ajaxDel")
    @ResponseBody
    public Result del2(Integer id){
        boolean flag = cartService.removeById(id);
        if(flag)
            return new Result("200","订单支付成功!",null);
        return new Result("500","订单支付失败!",null);
    }
}
